﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Data;

/// <summary>
/// Summary description for StatisticsBusiness
/// </summary>
public class StatisticsBusiness
{
	public StatisticsBusiness()
	{
		//
		// TODO: Add constructor logic here
		//
	}
    public bool Insert(StatisticsEntities se)
    {
        bool flat = false;
        string[] paras = { "@statisticsDate", "@statisticsVisited" };
        string[] values = { se._statisticsDate,se._statisticsVisited.ToString() };
        if (DataAccess.getExecProcedure("sp_InsertStatistics", paras, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Insert(string[] values)
    {
        bool flat = false;
        string[] paras = { "@statisticsDate", "@statisticsVisited" };        
        if (DataAccess.getExecProcedure("sp_InsertStatistics", paras, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Update(StatisticsEntities se)
    {
        bool flat = false;
        string[] paras = {  "@statisticsDate", "@statisticsVisited" };
        string[] values = { se._statisticsDate, se._statisticsVisited.ToString() };
        if (DataAccess.getExecProcedure("sp_UpdateStatistics", paras, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Update(string[] values)
    {
        bool flat = false;
        string[] paras = {  "@statisticsDate", "@statisticsVisited" };        
        if (DataAccess.getExecProcedure("sp_UpdateStatistics", paras, values) > 0)
            flat = true;
        else
            flat = false;
        return flat;
    }

    public bool Delete(int statisticsID)
    {
        if (DataAccess.getExec("sp_DeleteStatistics '" + statisticsID + "'") > 0)
            return true;
        else
            return false;

    }

    public DataTable GetAllStatistics()
    {
        return DataAccess.DataTable_Query("sp_GetStatistics '0'");
    }

    public StatisticsEntities GetLastStatistics()
    {
        StatisticsEntities se = new StatisticsEntities();
        DataTable dt = this.GetAllStatistics();
        DataRow dr = dt.Rows[0];
        se._statisticsID = int.Parse(dr["statisticsID"].ToString());
        se._statisticsDate = dr["statisticsDate"].ToString();
        se._statisticsVisited = int.Parse(dr["statisticsVisited"].ToString());
        return se;
    }

    public StatisticsEntities GetStatistics(int statisticsID)
    {
        StatisticsEntities se = new StatisticsEntities();
        DataTable dt=DataAccess.DataTable_Query("sp_GetStatistics '"+statisticsID+"'");
        if (dt != null)
        {
            DataRow dr = dt.Rows[0];
            se._statisticsID = int.Parse(dr["statisticsID"].ToString());
            se._statisticsDate = dr["statisticsDate"].ToString();
            se._statisticsVisited =int.Parse(dr["statisticsVisited"].ToString());
        }
        return se;
    }
}